﻿using ReflectionStudio.Core.Events;
using System.Collections.Generic;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Chart;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for UserControl2.xaml
    /// </summary>
    public partial class UIWB19 : ViewBase
    {
        #region ==FWB08KEY===========
        public static readonly DependencyProperty FWB08KEYProperty = DependencyProperty.Register("FWB08KEY",
            typeof(string), typeof(UIWB19),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFWB08KEYChanged));
        public string FWB08KEY
        {
            get
            {
                return (string)GetValue(FWB08KEYProperty);
            }
            set
            {
                SetValue(FWB08KEYProperty, value);

            }
        }

        private static void OnFWB08KEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.FWB08KEY = (string)e.NewValue;
                src.OnFWB08KEYChg((string)e.NewValue);
            }
        }
        public virtual void OnFWB08KEYChg(string value)
        {

        }
        #endregion
        #region ==HPOSITION===========
        public static readonly DependencyProperty HPOSITIONProperty = DependencyProperty.Register("HPOSITION",
            typeof(string), typeof(UIWB19),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnHPOSITIONChanged));
        public string HPOSITION
        {
            get
            {
                return (string)GetValue(HPOSITIONProperty);
            }
            set
            {
                SetValue(HPOSITIONProperty, value);

            }
        }

        private static void OnHPOSITIONChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.HPOSITION = (string)e.NewValue;
                src.OnHPOSITIONChg((string)e.NewValue);
            }
        }
        public virtual void OnHPOSITIONChg(string value)
        {

        }
        #endregion

        public bool IsAutoNext = true;
        public bool IsLengStart = false;
        public VWBFEHL TMV
        {
            get
            {
                return (VWBFEHL)MV;
            }
        }

        #region ==IsChkAll===========
        public static readonly DependencyProperty IsChkAllProperty = DependencyProperty.Register("IsChkAll",
            typeof(bool), typeof(UIWB19),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsChkAllChanged));
        public bool IsChkAll
        {
            get
            {
                return (bool)GetValue(IsChkAllProperty);
            }
            set
            {
                SetValue(IsChkAllProperty, value);
            }
        }
        private static void OnIsChkAllChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.IsChkAll = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==CPCS===========
        public static readonly DependencyProperty CPCSProperty = DependencyProperty.Register("CPCS",
            typeof(int), typeof(UIWB19),
            new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCPCSChanged));
        public int CPCS
        {
            get
            {
                return (int)GetValue(CPCSProperty);
            }
            set
            {
                SetValue(CPCSProperty, value);
            }
        }
        private static void OnCPCSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.CPCS = (int)e.NewValue;
            }
        }
        #endregion 

        #region ==WBFQPKT===========
        public static readonly DependencyProperty WBFQPKTProperty = DependencyProperty.Register("WBFQPKT",
            typeof(string), typeof(UIWB19),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWBFQPKTChanged));
        public string WBFQPKT
        {
            get
            {
                return (string)GetValue(WBFQPKTProperty);
            }
            set
            {
                SetValue(WBFQPKTProperty, value);
                if (MV == null)
                    return;
                if (value == "")
                    return;
                TMV.WBFQPKT = double.Parse(value);
            }
        }
        private static void OnWBFQPKTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.WBFQPKT = (string)e.NewValue;
            }
        }
        #endregion

        #region ==DFFSCHL===========
        public static readonly DependencyProperty DFFSCHLProperty = DependencyProperty.Register("DFFSCHL",
            typeof(string), typeof(UIWB19),
            new FrameworkPropertyMetadata("   ", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDFFSCHLChanged));
        public string DFFSCHL
        {
            get
            {
                return (string)GetValue(DFFSCHLProperty);
            }
            set
            {
                SetValue(DFFSCHLProperty, value);
                OnDFFSCHLChg();
            }
        }

        private static void OnDFFSCHLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.DFFSCHL = (string)e.NewValue;
            }
        }

        public virtual void OnDFFSCHLChg()
        {
        }

        #endregion

        #region ==ERVER===========
        public static readonly DependencyProperty ERVERProperty = DependencyProperty.Register("ERVER",
            typeof(int), typeof(UIWB19),
            new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnERVERChanged));
        public int ERVER
        {
            get
            {
                return (int)GetValue(ERVERProperty);
            }
            set
            {
                SetValue(ERVERProperty, value);
                OnERVERChg();
            }
        }

        private static void OnERVERChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.ERVER = (int)e.NewValue;
            }
        }

        public virtual void OnERVERChg()
        {
        }

        #endregion

        #region ==EKEY===========
        public static readonly DependencyProperty EKEYProperty = DependencyProperty.Register("EKEY",
            typeof(string), typeof(UIWB19),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEKEYChanged));
        public string EKEY
        {
            get
            {
                return (string)GetValue(EKEYProperty);
            }
            set
            {
                SetValue(EKEYProperty, value);

            }
        }
        private static void OnEKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.EKEY = (string)e.NewValue;
                src.OnEKEYChg();
            }
        }

        #endregion

        #region ==ERKEY===========
        public static readonly DependencyProperty ERKEYProperty = DependencyProperty.Register("ERKEY",
            typeof(string), typeof(UIWB19),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnERKEYChanged));
        public string ERKEY
        {
            get
            {
                return (string)GetValue(ERKEYProperty);
            }
            set
            {
                SetValue(ERKEYProperty, value);

            }
        }
        private static void OnERKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.ERKEY = (string)e.NewValue; 
            }
        }

        #endregion 

        #region ==EXEKEY===========
        public static readonly DependencyProperty EXEKEYProperty = DependencyProperty.Register("EXEKEY",
            typeof(string), typeof(UIWB19),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXEKEYChanged));
        public string EXEKEY
        {
            get
            {
                return (string)GetValue(EXEKEYProperty);
            }
            set
            {
                SetValue(EXEKEYProperty, value);
                OnEXEKEYChg();
            }
        }
        private static void OnEXEKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.EXEKEY = (string)e.NewValue;
            }
        }
        public virtual void OnEXEKEYChg()
        {
        }
        #endregion

        #region ==RWAUF===========
        public static readonly DependencyProperty RWAUFProperty = DependencyProperty.Register("RWAUF",
            typeof(string), typeof(UIWB19),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRWAUFChanged));
        public string RWAUF
        {
            get
            {
                return (string)GetValue(RWAUFProperty);
            }
            set
            {
                SetValue(RWAUFProperty, value);
                OnRWAUFChg(value);
            }
        }
        private static void OnRWAUFChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.RWAUF = (string)e.NewValue;
            }
        }
        #endregion

        #region ==CMTR===========
        public static readonly DependencyProperty CMTRProperty = DependencyProperty.Register("CMTR",
            typeof(double), typeof(UIWB19),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCMTRChanged));
        public double CMTR
        {
            get
            {
                return (double)GetValue(CMTRProperty);
            }
            set
            {
                SetValue(CMTRProperty, value);
            }
        }
        private static void OnCMTRChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.CMTR = (double)e.NewValue;
            }
        }
        #endregion
        #region ==ERRLNG===========
        public static readonly DependencyProperty ERRLNGProperty = DependencyProperty.Register("ERRLNG",
            typeof(double), typeof(UIWB19),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnERRLNGChanged));
        public double ERRLNG
        {
            get
            {
                return (double)GetValue(ERRLNGProperty);
            }
            set
            {
                SetValue(ERRLNGProperty, value);
            }
        }
        private static void OnERRLNGChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.ERRLNG = (double)e.NewValue;
                src.OnERRLNGChg((double)e.NewValue);
            }
        }
        public virtual void OnERRLNGChg(double errlng)
        {

        }
        #endregion



        #region ==RVEKEY===========
        public static readonly DependencyProperty RVEKEYProperty = DependencyProperty.Register("RVEKEY",
            typeof(string), typeof(UIWB19),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRVEKEYChanged));
        public string RVEKEY
        {
            get
            {
                return (string)GetValue(RVEKEYProperty);
            }
            set
            {
                SetValue(RVEKEYProperty, value);
                OnRVEKEYChg();
            }
        }
        private static void OnRVEKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.RVEKEY = (string)e.NewValue;
            }
        }

        #endregion

        #region ==IsFinish===========
        public static readonly DependencyProperty IsFinishProperty = DependencyProperty.Register("IsFinish",
            typeof(bool), typeof(UIWB19),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsFinishChanged));
        public bool IsFinish
        {
            get
            {
                return (bool)GetValue(IsFinishProperty);
            }
            set
            {
                SetValue(IsFinishProperty, value);
            }
        }
        private static void OnIsFinishChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.IsFinish = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==dwNFError===========
        public static readonly DependencyProperty dwNFErrorProperty = DependencyProperty.Register("dwNFError",
            typeof(DataRow), typeof(UIWB19),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwNFErrorChanged));
        public DataRow dwNFError
        {
            get
            {
                return (DataRow)GetValue(dwNFErrorProperty);
            }
            set
            {
                SetValue(dwNFErrorProperty, value);
                OnNEWFError();
            }
        }
        private static void OndwNFErrorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.dwNFError = (DataRow)e.NewValue;
            }
        }
        public virtual void OnNEWFError()
        {
        }
        #endregion


        #region ==TTPOINTS===========
        public static readonly DependencyProperty TTPOINTSProperty = DependencyProperty.Register("TTPOINTS",
            typeof(double), typeof(UIWB19),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTTPOINTSChanged));
        public double TTPOINTS
        {
            get
            {
                return (double)GetValue(TTPOINTSProperty);
            }
            set
            {
                SetValue(TTPOINTSProperty, value);
            }
        }
        private static void OnTTPOINTSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.TTPOINTS = (double)e.NewValue;
            }
        }
        #endregion 
        public ChartEx0008 Chart08
        {
            get
            {
                return this.FindName("Chart08") as ChartEx0008;
            }
        }
        #region ==SHIFTPRODUCTION===========
        public static readonly DependencyProperty SHIFTPRODUCTIONProperty = DependencyProperty.Register("SHIFTPRODUCTION",
            typeof(string), typeof(UIWB19),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSHIFTPRODUCTIONChanged));
        public string SHIFTPRODUCTION
        {
            get
            {
                return (string)GetValue(SHIFTPRODUCTIONProperty);
            }
            set
            {
                SetValue(SHIFTPRODUCTIONProperty, value);

                OnShifProdChg();
            }
        }
        private static void OnSHIFTPRODUCTIONChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWB19;
            if (src != null)
            {
                src.SHIFTPRODUCTION = (string)e.NewValue;
            }
        }
        public virtual void OnShifProdChg()
        { }
        #endregion
        public UIWB19()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
        }
        public override void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
            }
            else
            {
                KEY = TMV.WBFRWKRS + TMV.WBFRWLFD + TMV.WBFRWPOS + TMV.WBFVERSION + TMV.WBFFPOS;
                ERKEY = TMV.WBFFSCHL;
            }
        }
        public virtual void OnRWAUFChg(string auf)
        {
            TBMain.Clear();
            if (auf.Length < 12)
                return;
            dwData = TBMain.NewRow();
            dwData["WBFMANDANT"] = app.Mandant;
            dwData["WBFRWKRS"] = auf.Substring(0, 2);
            dwData["WBFRWLFD"] = auf.Substring(2, 6);
            dwData["WBFRWPOS"] = auf.Substring(8, 4);
            SResult = TMV.QWBFEHL02(dwData);
            MergeData(SResult, true, true);
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            string key = KEY;
            filter = "WBFRWKRS+WBFRWLFD+WBFRWPOS+WBFVERSION+WBFFPOS='" + KEY + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                TMV.DWFE01(row);
                row.Delete();
            }
            TBMain.AcceptChanges();
            RVEKEY = "";
            RVEKEY = key;
            base.DeleteCommand(sender, e);
        }
        public virtual void OnRVEKEYChg()
        {
            try
            {
                if (RVEKEY == "")
                    return;
                filter = "WBFFPOS=" + RVEKEY.Substring(13);
                rows = TBMain.Select(filter);
                foreach (DataRow row in rows)
                {
                    TMV.DWFE01(row);
                    row.Delete();
                }
                TBMain.AcceptChanges();
                TTPOINTS = TMV.SUM1;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void StartError(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.WBFLGVON = CMTR;
            TMV.WBFLGBIS = CMTR;
        }
        public void EndError(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (ERRLNG > 0)
            {
                TMV.WBFLGBIS = TMV.WBFLGVON + ERRLNG;
                ERRLNG = 0;
            }
            else
            {
                TMV.WBFLGBIS = CMTR;

            }
            PM["WBFLGBIS"] = TMV.WBFLGBIS;
            PM["WBFLGVON"] = TMV.WBFLGVON;
            PM["V3"] = EKEY.Substring(10, 2);
            PM["V4"] = EKEY.Substring(20, 2);
            PM["V5"] = EKEY.Substring(30, 2);
            CHKPOINT(PM);
            if (sender == null)
                return;
            vwMaster.View.MoveCurrentToFirst();
        }
        public void DFCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            DFFSCHL = TMV.WBFFSCHL;
        }

        /// <summary>
        /// 疵点统计扣分的话，默认4分，长度计量的话
        /// 1-1.5米的是10分，1.5-2米的是15分，
        /// 2-2.5米的是20分，2.5-3米的是25分，
        /// 3-3.5米的是30分，3.5-4米的是35分，
        /// 4-4.5米的是40分，最大40分。

        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        public virtual void CHKPOINT(Dictionary<string,object>pm)
        {
            
            switch (app.company)
            {
                default:
                case "SYH":
                    WBFQPKT = TMV.CHKSYHPOINT(PM);
                    break;
                case "KWT":
                    WBFQPKT = TMV.CHKKWTPOINT(PM);
                    break;
                case "UBT":
                    WBFQPKT = TMV.CHKRUBTPOINT(PM);
                    break;
            }
        }
        public void SetUpChart()
        {
            try
            {

                DataTable data;
                string name = "统计";
                string X = "WBFFSCHL";
                string Y = "WBFQPKT";
                name = MV.month;
                Chart08.vm.IsRowColumnSwitched = false;
                Chart08.vm.IsTitleVisible = false;
                Chart08.vm.IsLegendVisible = true;
                Chart08.vm.DefaultTitle = "疵点统计";
                Chart08.vm.DefaultSubTitle = "分数";

                Chart08.RATIO = 1;
                Chart08.Converter = 1;
                Chart08.SetUp(1, name, TBMain, X, Y);
                Chart08.Confirm();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    return;
                if (TMV.WBFLGVON < 0)
                {
                    TMV.WBFLGVON = CMTR;
                    TMV.WBFLGBIS = CMTR;
                }
                string v4 = "00";
                if (TMV.WBFQPKT == 50)
                    v4 = "50";
                PM["WBFLGBIS"] = TMV.WBFLGBIS;
                PM["WBFLGVON"] = TMV.WBFLGVON;
                PM["V3"] =EKEY.Substring(10,2);
                PM["V4"] = v4;
                PM["V5"] = EKEY.Substring(30, 2);
                CHKPOINT(PM);
                foreach (DataRow row in TBMain.Rows)
                {
                    TMV.UpdateWBFEHL(row, 0);
                }
                if (IsCanNew
                  & !IsAutoNext)//是否等待输入
                {
                    EndError(null, null);
                    NewCommand(null, null);
                }
                vwMaster.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {

            }
            IsAutoNext = true;
            //base.SaveCommand(sender, e);

        }
        public void chkAll_Click(object sender, RoutedEventArgs e)
        {
            IsChkAll = !IsChkAll;
        }
        public virtual void OnEKEYChg()
        {
            if (EKEY.Length == 0)
                return;
            if (IsLengStart)
            {
                if (TMV.WBFLGBIS < 0)
                {
                    TMV.WBFLGBIS = CMTR;
                }

            }
            else
            {
                if (TMV.WBFLGBIS < 0)
                {
                    TMV.WBFLGVON = CMTR;
                    TMV.WBFLGBIS = CMTR;
                }
            }
            TMV.WBFFSCHL = EKEY.PadRight(20).Substring(0, 3);
            switch (EKEY.Substring(10, 10).Trim())
            {
                //0是短，1是长，2，记长，3.自动长，4记条，5均个数
                case "02":
                    INFO = "请输入疵点长度";
                    IsAutoNext = false;
                    break;
                case "03":
                    INFO = "疵点默认长度为:" + EKEY.Substring(30, 10).Trim() + "CM";
                    break;
                case "04":
                    INFO = "默认分值:" + EKEY.Substring(20, 10).Trim() + "分";
                    break;
                case "05":
                    INFO = "请输入平均个数";
                    IsAutoNext = false;
                    break;
            }
        }
    }
}
